<?php

namespace app\api\controller;

use app\admin\model\Meet as MeetModel;
use app\common\controller\Api;

/**
 * 会务记录
 */
class Meet extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['index', 'meetDetail', 'analysis', 'analysisList'];
    
    /**
     * 会务记录列表
     */
    public function index()
    {
        $page = $this->request->param('page/d', 1);
        $list = MeetModel::field(['id', 'address', 'type', 'name'])
            ->withCount([
                'baoming',
                'signed',
                'signbill'
            ])
            ->order('id DESC')
            ->page($page, $this->limit)
            ->select();
        return $this->success('ok', compact('list'));
    }

    /**
     * 会务详情
     */
    public function meetDetail()
    {
        $id = $this->request->param('id/d');
        $page = $this->request->param('page/d', 1);
        $type = $this->request->param('type/d', 1); // 1报名人数 2签到人数 3签单人数

        /** @var MeetModel $detail */
        $detail = MeetModel::field(['id', 'name'])->where('id', $id)->find();
        if (empty($detail)) {
            return $this->error('数据未找到！');
        }
        switch ($type) {
            case 1:
                $query = $detail->baoming();
                break;
            case 2:
                $query = $detail->signed();
                break;
            case 3:
                $query = $detail->signbill();
                break;
            default:
                $query = $detail->baoming();
                break;
        }
        $list = $query->page($page, $this->limit)
            ->field([
                'id', 'username', 'sex', 'mobile', 'pay_price'
            ])->select();
        return $this->success('ok', compact('detail', 'list'));
    }

    /**
     * 会务数据统计
     *
     * @return void
     */
    public function analysis()
    {
        $year = $this->request->param('year/d', date('Y'));
        $season = $this->request->param('season/d', ceil((date('n')) / 3));
        [$start, $end] = getQuarter($year, $season);
        $list = $list = MeetModel::field(['id', 'address', 'type', 'name'])
            ->withCount([
                'baoming',
                'signed',
                'signbill',
                'prize_log'
            ])
            ->whereTime('createtime', 'between', [$start, $end])
            ->order('id DESC')
            ->select();
        $column = [
            'categories' => array_column($list, 'name'),
            'series' => [
                [
                    'name' => '报名人数',
                    'data' => array_column($list, 'baoming_count'),
                ],
                [
                    'name' => '签到人数',
                    'data' => array_column($list, 'signed_count'),
                ],
                [
                    'name' => '奖品兑换数',
                    'data' => array_column($list, 'prize_log_count'),
                ],
                [
                    'name' => '签单量',
                    'data' => array_column($list, 'signbill_count'),
                ]
            ]
        ];
        return $this->success('ok', compact('list', 'year', 'season', 'column'));
    }

    /**
     * 统计列表
     */
    public function analysisList()
    {
        $page = $this->request->param('page/d', 1);
        $list = MeetModel::field(['id', 'address', 'type', 'name'])
            ->withCount([
                'baoming',
                'signed',
                'signbill',
                'prize_log'
            ])
            ->order('id DESC')
            ->page($page, $this->limit)
            ->select();
        return $this->success('ok', compact('list'));
    }
}
